package com.ymatou.iapi.exp.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ymatou.iapi.exp.parameter.GetUserCoinRecordsBean;
import com.ymatou.iapi.exp.service.GetUserCoinRecordsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ExpCallService;
import com.ymttest.database.model.UserCoinRecord;
import com.ymttest.database.sqlwapper.ExpWapper;
import com.ymttest.utils.YMTDateUtil;
/**
*查询获取金币记录
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetUserCoinRecords{
	private static GetUserCoinRecordsBean getusercoinrecordsBean;
	private static GetUserCoinRecordsCall getusercoinrecordsCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询获取金币记录");
	}

	@Before
	public void caseUp() {
		getusercoinrecordsBean=new GetUserCoinRecordsBean();
		getusercoinrecordsCall=new GetUserCoinRecordsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getusercoinrecordsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB() {
		JsonObject result = getusercoinrecordsCall.getJsonObject().get("Data").getAsJsonObject();
		JsonArray returnrecords = result.get("records").getAsJsonArray();
		ExpWapper ExpWapper=new ExpWapper();
		List<UserCoinRecord> records = ExpWapper.selectUserCoinRecord(getusercoinrecordsBean.getUserId(), getusercoinrecordsBean.getOperateType());
		Logger.verifyEquals(result.get("totals").getAsInt(), records.size(), "验证数量");
		int begin=(getusercoinrecordsBean.getPageIndex()-1)*getusercoinrecordsBean.getPageSize();
		int offset=0;
		for(int i=begin;i<getusercoinrecordsBean.getPageSize();i++){
			UserCoinRecord record = records.get(i);
			//{"coin":-10,"datetime":"2018-03-06 15:55:58","description":"抽奖扣除金币"},
			 JsonObject returnc = returnrecords.get(offset).getAsJsonObject();
			Logger.verifyEquals(record.getTotalCoin(),returnc.get("coin").getAsInt(),"验证coin");
			String ct = YMTDateUtil.getFormatTime(record.getCreateTime(), "yyyy-MM-dd HH:mm:ss");
			Logger.verifyEquals(ct,returnc.get("datetime").getAsString(),"验证datetime");
			Logger.verifyNotNull(returnc.get("title").getAsString(),"验证title");
			//description可能为空但是不为null
			Logger.verifyEquals(true,returnc.get("description").getAsString()!=null,"验证description");
			offset++;
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUserCoinRecords_001() {
		Logger.start(true,"查询获取金币记录operateType0成功");
		try {
			int uid =101;
			ExpCallService.accountCoin(uid,10);
			getusercoinrecordsBean.setOperateType(0);
			getusercoinrecordsBean.setUserId(uid);
			getusercoinrecordsBean.setPageIndex(1);
			getusercoinrecordsBean.setPageSize(10);
			getusercoinrecordsCall.setData(getusercoinrecordsBean);
			getusercoinrecordsCall.callService();			
			JsonObject result = getusercoinrecordsCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("0", getusercoinrecordsCall.getString("BCode"), "验证BCode");
			verifyDB();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserCoinRecords_002() {
		Logger.start(true,"查询获取金币记录operateType1成功");
		try {
			int uid =101;
			ExpCallService.accountCoin(uid,10);
			getusercoinrecordsBean.setOperateType(1);
			getusercoinrecordsBean.setUserId(uid);
			getusercoinrecordsBean.setPageIndex(1);
			getusercoinrecordsBean.setPageSize(10);
			getusercoinrecordsCall.setData(getusercoinrecordsBean);
			getusercoinrecordsCall.callService();			
			JsonObject result = getusercoinrecordsCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("0", getusercoinrecordsCall.getString("BCode"), "验证BCode");
			verifyDB();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserCoinRecords_003() {
		Logger.start(true,"查询获取金币记录operateType2成功");
		try {
			int uid =101;
			ExpCallService.accountCoin(uid,10);
			getusercoinrecordsBean.setOperateType(2);
			getusercoinrecordsBean.setUserId(uid);
			getusercoinrecordsBean.setPageIndex(1);
			getusercoinrecordsBean.setPageSize(10);
			getusercoinrecordsCall.setData(getusercoinrecordsBean);
			getusercoinrecordsCall.callService();			
			JsonObject result = getusercoinrecordsCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("0", getusercoinrecordsCall.getString("BCode"), "验证BCode");		
			verifyDB();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserCoinRecords_004() {
		Logger.start(true,"获取多条记录成功");
		try {
			int uid =101;
			ExpCallService.accountCoin(uid,10);
			getusercoinrecordsBean.setOperateType(0);
			getusercoinrecordsBean.setUserId(uid);
			getusercoinrecordsBean.setPageIndex(1);
			getusercoinrecordsBean.setPageSize(10);
			getusercoinrecordsCall.setData(getusercoinrecordsBean);
			getusercoinrecordsCall.callService();			
			JsonObject result = getusercoinrecordsCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("0", getusercoinrecordsCall.getString("BCode"), "验证BCode");
			verifyDB();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserCoinRecords_005() {
		Logger.start(true,"获取翻页记录成功");
		try {
			int uid =101;
			ExpCallService.accountCoin(uid,10);
			getusercoinrecordsBean.setOperateType(0);
			getusercoinrecordsBean.setUserId(uid);
			getusercoinrecordsBean.setPageIndex(2);
			getusercoinrecordsBean.setPageSize(5);
			getusercoinrecordsCall.setData(getusercoinrecordsBean);
			getusercoinrecordsCall.callService();			
			JsonObject result = getusercoinrecordsCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("0", getusercoinrecordsCall.getString("BCode"), "验证BCode");
			verifyDB();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserCoinRecords_006() {
		Logger.start(true,"不输入uid失败");
		try {
			getusercoinrecordsBean.setOperateType(0);
			getusercoinrecordsBean.setPageIndex(2);
			getusercoinrecordsBean.setPageSize(5);
			getusercoinrecordsCall.setData(getusercoinrecordsBean);
			getusercoinrecordsCall.callService();			
			JsonObject result = getusercoinrecordsCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("false", result.get("success").toString(), "验证success");
			Logger.verifyEquals("2", getusercoinrecordsCall.getString("Code"), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}